package com.ces.knacky.restws.client;

import java.io.ByteArrayInputStream;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.webdav.lib.methods.PostMethod;




public class TestAuctionRService {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		//add xml
		/*final String addJson = "<auction>" +
		  "<description>auction6</description>" + 
		  "<createTime>2010-12-10T17:04:42+05:30</createTime>" + 
		  "<updateTime>2010-12-10T17:04:42+05:30</updateTime>" + 
		  "<user>" +
		  "<id>1</id>" + 
		  "</user>" +
		  "<flag>" +
		  "<id>1</id>" + 
		  "</flag>" +
		  "<endDate>2010-01-02T00:00:00+05:30</endDate>" + 
		  "<endTime>12</endTime>" + 
		  "<startDate>2010-01-01T00:00:00+05:30</startDate>" + 
		  "<startTime>12</startTime>" + 
		  "<summary>auction6</summary>" + 
		  "</auction>";*/
 
		final String addJson = "<auction>" +
		 "<id>1</id>" + 
		  "<description>auction6_upd</description>" + 
		  "<createTime>2010-12-10T17:04:42+05:30</createTime>" + 
		  "<updateTime>2010-12-10T17:04:42+05:30</updateTime>" + 
		  "<user>" +
		  "<id>1</id>" + 
		  "</user>" +
		  "<flag>" +
		  "<id>1</id>" + 
		  "</flag>" +
		  "<endDate>2010-01-02T00:00:00+05:30</endDate>" + 
		  "<endTime>12</endTime>" + 
		  "<startDate>2010-01-01T00:00:00+05:30</startDate>" + 
		  "<startTime>12</startTime>" + 
		  "<summary>auction6_upd</summary>" + 
		  "</auction>";
		
		//update json
		//final String addJson = "{\"id\":\"3\",\"description\":\"auction3_updt\",\"user\":{\"id\":\"1\"},\"flag\":{\"id\":\"1\"},\"endDate\":\"2010-01-02T00:00:00+05:30\",\"endTime\":\"12\",\"startDate\":\"2010-01-01T00:00:00+05:30\",\"startTime\":\"12\",\"summary\":\"auction3_updt\"}";
		
		//add json
		//final String addJson = "{\"description\":\"auction6\",\"user\":{\"id\":\"1\"},\"flag\":{\"id\":\"1\"},\"endDate\":\"2010-01-02T00:00:00+05:30\",\"endTime\":\"12\",\"startDate\":\"2010-01-01T00:00:00+05:30\",\"startTime\":\"12\",\"summary\":\"auction6\"}";
		
		 RequestEntity entity = new InputStreamRequestEntity (  

			      new ByteArrayInputStream(addJson.getBytes()),  

			       "application/json");

		
		
		
		
				

			   //Add Auction
		 
		 		//PostMethod postMethod = new PostMethod("http://localhost:8080/eAuction/rest/auctionService/add");
		 
		 		//Add for cloud foundry
		 		//PostMethod postMethod = new PostMethod("http://ec2-204-236-203-152.compute-1.amazonaws.com/eAuction/rest/auctionService/add");
		 		//Update Auction
		 		PutMethod putMethod = new PutMethod("http://localhost:8080/eAuction/rest/auctionService/update");
		
		 		//Delete Auction
	 			DeleteMethod deleteMethod = new DeleteMethod("http://localhost:8080/eAuction/rest/auctionService/3");
		 
			   //Place Bid
			   //PostMethod postMethod = new PostMethod("http://localhost:8080/eAuction/rest/auctionService/4/item/15/bidPrice/20");
			   
			   //Approve Bid
			   //PostMethod postMethod = new PostMethod("http://localhost:8080/eAuction/rest/auctionService/bid/approve/2,3");
			   
			 //Load Placed Bid
			   GetMethod getMethod = new GetMethod("http://localhost:8080/eAuction/rest/auctionService/bid/place");
			   

		

			     

			   HttpClient client = new HttpClient();  

			  try {  
				  int result;
				 
				  //update
				  putMethod.setRequestHeader("Content-Type", "application/xml");
				  
				  putMethod.setRequestEntity(entity);
	    	      
				  result = client.executeMethod(putMethod);
				  
				  System.out.println(putMethod.getResponseBodyAsString(4000));
				  
				  //Add
				  
				  //postMethod.setRequestHeader("Content-Type", "application/xml");
				  
				  //postMethod.setRequestEntity(entity);
	    	      
				  //result = client.executeMethod(postMethod);
				  
				  //System.out.println(postMethod.getResponseBodyAsString(4000));
				  
				 	
				  //result = client.executeMethod(getMethod);
				  //System.out.println(getMethod.getResponseBodyAsString(5000));
				  
				  
				  //System.out.println(postMethod.getResponseBodyAsString(4000));
				  //putMethod.setRequestEntity(entity);
	    	      
				  /*result = client.executeMethod(deleteMethod);
				  
				  System.out.println(deleteMethod.getResponseBodyAsString(5000));*/
			      
			 
			  }
			  catch(Exception ex)
			  {
				  ex.printStackTrace();
			  }
			  /*finally {  

			     deleteMethod.releaseConnection();  

			  }  
*/

	

}
	
}
